package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HosptialRepository;
import com.atguigu.yygh.hosp.service.HosptialService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/*==========================================
      时 间:   2022-03-07
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@Service
public class HosptialServiceImpl implements HosptialService {

    @Autowired
    private HosptialRepository hosptialRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        Hospital paramHospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        String hoscode = (String)paramMap.get("hoscode");
        Hospital hospital= hosptialRepository.findByHoscode(hoscode);
        if(hospital != null){//更新操作
            paramHospital.setCreateTime(hospital.getCreateTime());
            paramHospital.setUpdateTime(new Date());
            paramHospital.setIsDeleted(hospital.getIsDeleted());
            paramHospital.setStatus(hospital.getStatus());
            paramHospital.setId(hospital.getId());
            hosptialRepository.save(paramHospital);
        }else{ //添加操作
            paramHospital.setCreateTime(new Date());
            paramHospital.setUpdateTime(new Date());
            paramHospital.setIsDeleted(0);
            paramHospital.setStatus(0);
            hosptialRepository.save(paramHospital);
        }

    }

    @Override
    public Map<String, Object> getHospitalByHoscode(String hoscode) {
        Hospital byHoscode = hosptialRepository.findByHoscode(hoscode);
        return JSONObject.parseObject(JSONObject.toJSONString(byHoscode), Map.class);
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.ASC, "createTime"));
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example<Hospital> example=Example.of(hospital,matcher);
        Page<Hospital> all = hosptialRepository.findAll(example, pageRequest);

        all.getContent().parallelStream().forEach(item->{
            this.packageHospital(item);
        });

        return all;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status == 0 || status == 1){
            Hospital hospital= hosptialRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hosptialRepository.save(hospital);
        }
    }

    @Override
    public Hospital getHospitalDetailById(String id) {
        Hospital hospital = hosptialRepository.findById(id).get();
        this.packageHospital(hospital);
        return hospital;
    }


    @Override
    public List<Hospital> findByName(String name) {
        return  hosptialRepository.findByHosnameLike(name);
    }


    @Override
    public Hospital getHospitalDetailByHoscode(String hoscode) {
        Hospital hospital = hosptialRepository.findByHoscode(hoscode);
        packageHospital(hospital);
        return hospital;
    }

    private void packageHospital(Hospital item) {

        String proviceAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getProvinceCode()));
        String cityAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getCityCode()));
        String dictAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getDistrictCode()));
        String hospitalLevel= dictFeignClient.getAddressNameByValue(DictEnum.HOSTYPE.getDictCode(),Long.valueOf(item.getHostype()));

        item.getParam().put("hostypeString", hospitalLevel);
        item.getParam().put("fullAddress", proviceAddress + cityAddress + dictAddress + item.getAddress());

     }


}
